﻿Imports System.Data.SqlClient

Public Class frmPaymentManagerScheduleEdit

    Public SNo As Integer

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub frmPaymentManagerScheduleEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From PaymentsSchedule Where SNo=" & SNo, cnn)
            Dim Reader As SqlDataReader

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.DTPPayDate.Value = CDate(Reader.Item("PayDate"))
                Me.txtAmount.Text = CDbl(Reader.Item("Amount")).ToString("N2")
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If Me.txtAmount.Text.Trim.Length = 0 OrElse CDbl(Me.txtAmount.Text.Trim) = 0 Then
                MsgBox("Please enter a valid amount")
                Me.txtAmount.Focus()
            Else
                Me.Cursor = Cursors.WaitCursor
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Update PaymentsSchedule Set PayDate=@PayDate," & _
                                          "Amount=@Amount,SavedDate=GetDate(),SavedUser=@SavedUser Where SNo=" & SNo, cnn)

                cnn.Open()

                'Add values
                cmd.Parameters.AddWithValue("@PayDate", Me.DTPPayDate.Value.ToShortDateString & " 10:10:10 AM")
                cmd.Parameters.AddWithValue("@Amount", CDbl(Me.txtAmount.Text.Trim))
                cmd.Parameters.AddWithValue("@SavedUser", CurrentUser)

                cmd.ExecuteNonQuery()
                cnn.Close()

                Me.Cursor = Cursors.Default

                Me.Close()
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub txtAmount_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount.Validated
        Me.txtAmount.Text = CDbl(Me.txtAmount.Text).ToString("N2")
    End Sub

    Private Sub txtAmount_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtAmount.Validating
        If Me.txtAmount.Text.Trim.Length = 0 Then
            Me.txtAmount.Text = 0
        End If

        Dim currency As Decimal
        'Convert the current value to currency, with or without a currency symbol.
        If Not Decimal.TryParse(Me.txtAmount.Text, Globalization.NumberStyles.Number, Nothing, currency) Then
            'Don't let the user leave the field if the value is invalid.
            With Me.txtAmount
                .HideSelection = False
                .SelectAll()
                MessageBox.Show("Please enter a valid amount.", _
                                "Invalid Value", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)
                .HideSelection = True
            End With
            e.Cancel = True
        End If
    End Sub
End Class